package cc.rengu.igas.channel.upac.core.service.trans;


import cc.rengu.igas.channel.upac.common.constant.UpacTreeNodeConstant;
import cc.rengu.igas.channel.upac.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.upac.core.model.ChnlBizResponse;
import cc.rengu.igas.channel.upac.core.service.base.AuthService;
import cc.rengu.igas.channel.upac.facade.request.OpenAcctAuthRequest;
import cc.rengu.igas.channel.upac.facade.response.OpenAcctAuthResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.entity.DstChannelInfo;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;

/**
 * 开户认证.
 */
public class OpenAcctAuthService extends AuthService {


    @Override
    protected Object beforeBizProcess() throws Exception {
        OpenAcctAuthRequest openAcctAuthRequest = new OpenAcctAuthRequest();
        ConvertUtil.convertOutput(openAcctAuthRequest);
        return openAcctAuthRequest;
    }

    @Override
    protected ChnlBizResponse callBizService(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        int iReturnCode = 0;
        String acctEncFlag = xmlTreeUtil.getXmlTreeStringValue(UpacTreeNodeConstant.ACCT_ENC_FLAG);
        ChnlBizResponse chnlBizResponse = new ChnlBizResponse();
        OpenAcctAuthResponse openAcctAuthResponse = new OpenAcctAuthResponse();
        OpenAcctAuthRequest openAcctAuthRequest = (OpenAcctAuthRequest) request;
        String payAcctNo = openAcctAuthRequest.getPayAcctNo();
        DstChannelInfo dstChannelInfo = (DstChannelInfo) xmlTreeUtil.getXmlTreeObjectValue(UpacTreeNodeConstant.DST_CHANNEL_INFO, DstChannelInfo.class);
        if (AppParamConstant.YES.equals(acctEncFlag)) {
            openAcctAuthRequest.setPayAcctNo(encryptData(payAcctNo));
        }
        ConvertUtil.convertInput(openAcctAuthRequest);
        /*customer信息转换*/
        if (null != openAcctAuthRequest.getCustomerInfo()) {
            packageCustomerInfo(openAcctAuthRequest.getCustomerInfo(), openAcctAuthRequest.getPayAcctNo());
        }
        if (null != openAcctAuthRequest.getRiskCtrlInfo()) {
            packageRiskCtrlInfo(openAcctAuthRequest.getRiskCtrlInfo());
        }
        xmlTreeUtil.setXmlTreeStringValue(UpacTreeNodeConstant.txnType, "85");
        xmlTreeUtil.setXmlTreeStringValue(UpacTreeNodeConstant.txnSubType, "02");
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM, openAcctAuthRequest.getHeader().getTxnNum());
        iReturnCode = schedule.callService(dstChannelInfo.getDstServiceName(), 0);
        if (98 == iReturnCode) {
            rglog.error("调用银联全渠道服务<{}>超时，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            openAcctAuthResponse.setRespCode(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode());
            openAcctAuthResponse.setRespDesc(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespDesc());
            chnlBizResponse.setResult(openAcctAuthResponse);
            return chnlBizResponse;
        } else if (0 != iReturnCode) {
            rglog.error("调用银联全渠道服务<{}>失败，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            throw new BizException(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
        }
        ConvertUtil.convertOutput(openAcctAuthResponse);
        openAcctAuthResponse.setRespCode(convertRespCode(openAcctAuthResponse.getRespCode()));

        chnlBizResponse.setResult(openAcctAuthResponse);
        return chnlBizResponse;
    }

    @Override
    protected void afterBizProcess(ChnlBizResponse response) throws Exception {
        OpenAcctAuthResponse openAcctAuthResponse = (OpenAcctAuthResponse) response.getResult();
        ConvertUtil.convertInput(openAcctAuthResponse);
    }
}
